//
//  AEHomePageSubViewController.m
//  AESOLAR_IOS
//
//  Created by Bin Tang on 2024/10/18.
//  Copyright © 2024 Mr.Wang. All rights reserved.
//

#import "AEHomePageSubViewController.h"
#import "AEHomePageSubViewModel.h"
#import "AEHomePageSubTopView.h"
#import "AEHomePageSubViewCell.h"

@interface AEHomePageSubViewController ()
@property (nonatomic, strong) AEHomePageSubViewModel *viewModel;

@property (nonatomic, strong) AEHomePageSubTopView *headerView;

@end

@implementation AEHomePageSubViewController

- (void)dealloc {
    debugMethod();
}

- (void)initWithVar {
    [super initWithVar];
    
}

- (void)initWithViewModel {
    _viewModel = [AEHomePageSubViewModel new];
    [_viewModel setupData];
}

- (void)initWithSubView {
    [super initWithSubView];
    self.view.backgroundColor = kWhiteColor;
    self.baseTableView.backgroundColor = kWhiteColor;
    self.baseTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.baseTableView registerClass:[AEHomePageSubViewCell class] forCellReuseIdentifier:[AEHomePageSubViewCell className]];
//    self.baseTableView.contentOffset = CGPointMake(0, 300);
    self.baseTableView.contentInset = UIEdgeInsetsMake(kAEHomePageSubTopViewHeight, 0, 0, 0);
    self.baseTableView.contentOffset = CGPointMake(0, -kAEHomePageSubTopViewHeight);
}

- (void)initWithHeaderView {
    [super initWithHeaderView];
    
    CGRect frame = CGRectMake(0, 0, kScreenW, kAEHomePageSubTopViewHeight);
    _headerView = [[AEHomePageSubTopView alloc] initWithFrame:frame];
    [self.view addSubview:_headerView];
    
    [self.headerView updateWithViewData:self.viewModel];
}

- (void)doRefreshData {
    [super doRefreshData];
    
    self.viewModel.pageId = 1;
    
    [self fromServerRequestData];
}

- (void)doMoreRefreshData {
    [super doMoreRefreshData];
    
    [self fromServerRequestData];
}

- (void)fromServerRequestData {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self endAllRefreshing];
    });
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.viewModel.dataList.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kAEHomePageSubViewCellHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AEHomePageSubViewCell *cell = [AEHomePageSubViewCell cellForTableView:tableView indexPath:indexPath];
    [cell updateCellData:nil];
    
    return cell;
}

@end
